<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微博详情页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        #app {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            width: 600px;
            max-width: 100%;
            text-align: left;
        }
        h1 {
            font-size: 24px;
            margin-bottom: 10px;
        }
        h2 {
            font-size: 18px;
            margin-bottom: 10px;
        }
        p {
            font-size: 14px;
            margin-bottom: 10px;
        }
        hr {
            margin: 20px 0;
        }
        .comment {
            margin-bottom: 20px;
        }
        .comment h4 {
            font-size: 16px;
            margin: 0;
        }
        .comment span {
            color: red;
        }
        .comment span.right {
            float: right;
            color: #666;
        }
        textarea {
            width: 100%;
            height: 100px;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            background-color: #fff;
        }
        input[type="button"] {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }
        input[type="button"]:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<div id="app">
    <h1>微博详情页面</h1>
    <h2><span style="color: mediumvioletred">{{ weibo.nickname }}</span>: {{ weibo.content }}</h2>
    <p>发布于: {{ weibo.created }}</p>
    <hr>
    <div class="comment" v-for="c in arr">
        <h4>
            <span>{{ c.nickname }}评论道:</span> {{ c.content }}
            <span class="right">发布于：{{ c.created }}</span>
        </h4>
    </div>

    <textarea cols="30" rows="3" placeholder="评论内容..." v-model="comment.content"></textarea>
    <input type="button" value="评论" @click="insert()">
</div>

<!-- 引入axios和vue的框架文件 -->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "#app",
        data: {
            weibo: {},
            comment: { content: "" },
            arr: []
        },
        methods: {
            insert() {
                // 查看评论表:id(不需要传递)、content、created(服务器生成)
                // user_id(当前登录用户的id)、weibo_id(在weibo对象中获取)
                // 发布评论时需要把当前浏览的微博id添加到评论对象中
                v.comment.weiboId = v.weibo.id;
                // 发请求,发布评论
                axios.post("/v1/comment/insert", v.comment).then(function (response) {
                    // 未登录不能评论
                    if (response.data == 1) {
                        location.reload(); // 刷新页面
                    } else {
                        alert("请先登录");
                        location.href = "/login.html";
                    }
                });
            }
        },
        created: function () {
            axios.get("/v1/weibo/selectById" + location.search).then(function (response) {
                v.weibo = response.data;
            });

            // 发请求获取当前微博相关的评论
            // 上面的get()会开线程发请求,主线程会继续往下走
            axios.get("/v1/comment/selectByWeiboId" + location.search).then(function (response) {
                v.arr = response.data;
            });
        }
    });
</script>
</body>
</html>
